package mapEditor.interactor;
	
import mapEditor.map.MapCanvas;
import mapEditor.obj.PathObject;
import mapEditor.utils.MapLocation;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;

import org.json.JSONException;

// ********************************************************************
// *** CLASS: PathMouseAdapter
// ********************************************************************
public class DrawPathInteractor extends Interactor
{ public String getMouseModeMnemonic() { return("Draw Path"); }
  private PathObject polygon; // !null when drawing a Path
  private MapLocation prev;

  // ******************************************************************
  // *** CONSTRUCTOR: PathMouseAdapter
  // ******************************************************************
  public DrawPathInteractor(MapCanvas mapCanvas) { super(mapCanvas,DRAW_PATH); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public void paintMouseEffect(Graphics2D g,ImageObserver imageObserver)
  { if(polygon!=null) polygon.paint(g,imageObserver); }

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  protected void mouseDragged()
  {	if(polygon!=null)
	{ 
	  double dx=Math.abs(mouseCurrent.getX()-prev.getX());
	  double dy=Math.abs(mouseCurrent.getY()-prev.getY());
	  if((dx>5) || (dy>5))
	  { MapLocation p=mouseCurrent.copy();
	    polygon.add(p); prev=p;
	    xmapCanvas.repaint();
	  }
	}
  }

  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  protected void mouseMoved() {}

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  protected void mouseLeftPressed(MapLocation p)
  {	if(polygon==null)
    { try { polygon=new PathObject(objectModel,p); }
	  catch(JSONException e) { e.printStackTrace(); }
      prev=p.copy();   
    }
  }
  protected void mouseRightPressed(MapLocation p) {}

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  protected void mouseLeftReleased(MapLocation p)
  { if(polygon!=null)
	{ if(polygon.numberOfPoints()>2) drawObjectDone(polygon,"PATH");
	  polygon=null;
	}
  }
  protected void mouseRightReleased(MapLocation p) {}

}